<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ObatModel extends zulModel{

	function __construct(){
			parent::__construct();
			$this->table_name	= "obat";
			$this->primary_key	= "ID_OBAT";
			$this->order_by		= "ID_OBAT ASC"; 
	}
	function gridObat(){
		$this->db->select('*');
		$this->db->from('obat');
		$this->flexigrid->build_query();
		$return['records'] = $this->db->get();
		
		$this->db->select('*');
		$this->db->from('obat');
		$this->flexigrid->build_query(FALSE);
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	function rekapObat($id_obat){
		$return["keluar"]	= $this->db->query("SELECT ID_OBAT, SUM(JUMLAH*-1) AS JUMLAH FROM resep_obat WHERE ID_OBAT = ".$id_obat);
		$return["masuk"]	= $this->db->query("SELECT ID_OBAT,SUM(JUMLAH) AS JUMLAH FROM detail_penerimaan WHERE ID_OBAT = ".$id_obat);
		$return["adjust"]	= $this->db->query("SELECT ID_OBAT,SUM(IF(JENIS=1,JUMLAH,JUMLAH*-1)) AS JUMLAH FROM detail_penyesuaian JOIN penyesuaian ON penyesuaian.ID_PENYESUAIAN=detail_penyesuaian.ID_PENYESUAIAN WHERE ID_OBAT = ".$id_obat);
		return $return;
	}
	function get_tgl($tgl_awal,$tgl_akhir,$id){
		$query	= $this->db->query("
		select DISTINCT(transaksi.TANGGAL),obat.ID_OBAT from ( select TGL_PENERIMAAN AS TANGGAL from penerimaan_obat union select TGL_RESEP AS TANGGAL from resep union select TGL_PENYESUAIAN AS TANGGAL from penyesuaian ) transaksi left join penerimaan_obat on transaksi.TANGGAL = penerimaan_obat.TGL_PENERIMAAN left join resep on transaksi.TANGGAL = resep.TGL_RESEP left join penyesuaian on transaksi.TANGGAL = penyesuaian.TGL_PENYESUAIAN LEFT JOIN detail_penerimaan ON penerimaan_obat.ID_PENERIMAAN_OBAT = detail_penerimaan.ID_PENERIMAAN_OBAT LEFT JOIN resep_obat ON resep_obat.ID_PENGELUARAN=resep.ID_PENGELUARAN LEFT JOIN detail_penyesuaian ON detail_penyesuaian.ID_PENYESUAIAN=penyesuaian.ID_PENYESUAIAN LEFT JOIN history_harga ON history_harga.ID_HISTORY_HARGA=detail_penerimaan.ID_HISTORY_HARGA OR history_harga.ID_HISTORY_HARGA=resep_obat.ID_HISTORY_HARGA OR history_harga.ID_HISTORY_HARGA=detail_penyesuaian.ID_HISTORY_HARGA JOIN obat ON obat.ID_OBAT=history_harga.ID_OBAT WHERE obat.ID_OBAT=".$id." AND (TANGGAL >=\"".$tgl_awal."\" AND TANGGAL <= \"".$tgl_akhir."\") ORDER BY TANGGAL ");
		return $query;
	}
}
?>